package rad.framework.seam;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.jboss.seam.core.Expressions;
import org.jboss.seam.core.Expressions.ValueExpression;

public class ColumnType implements Serializable {
	private static final long serialVersionUID = 1L;

	public enum Type {
		String, TextArea, Boolean, Enum, Reference, Date, Time, Timestamp, Integer, Decimal, Currency, Percent, Link, Image, Blob, Secret
	}

	public String row = "row";
	public String name;
	public String label; // TODO ValueExpression ???
	public String type;
	private ValueExpression<Boolean> rendered;

	public ColumnType(String name, String label, String type, String rendered) {
		this.label = label;
		this.name = name;
		this.type = type;
		setRendered(rendered);
	}

	public Object getValue() {
		return Expressions.instance().createValueExpression(
				"#{" + row + "." + name + "}").getValue();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getRow() {
		return row;
	}

	public void setRow(String row) {
		this.row = row;
	}

	public boolean isRendered() {
		if (rendered == null) {
			return true;
		} else {
			Boolean value = (Boolean) rendered.getValue();
			if (value == null) {
				return true;
			} else {
				return value.booleanValue();
			}
		}
	}

	public void setRendered(String rendered) {
		if (!StringUtils.isEmpty(rendered)) {
			this.rendered = Expressions.instance().createValueExpression(rendered, Boolean.class);
		}
	}

	public String getRenderedExpressionString() {
		if (rendered == null) {
	 		return "true";
		} else {
	 		return rendered.getExpressionString();
		}
	}
}
